#!/bin/bash
source /home/lhy/.bash_profile
echo "try to restart guangxi  web  `date`"
#su - admin
cd /home/lhy/project/guangxi_sg/guangxiTrunk
#check out
echo "svn update..."
#get last update version
declare -i sgSid=$(jps -m|grep 8800|awk '{print $1}')
declare -i lastUpdateVersion=$(svn info|grep "最后修改的版本"|awk '{print $2}')
echo "当前版本号为 «$lastUpdateVersion"
svn up --username dingrui --password tianqueshuaige --accept 'theirs-conflict'
declare -i newUpdateVersion=$(svn info|grep "版本"|awk '{print $2}')
echo "新版号为 «$newUpdateVersion"
if [ "$lastUpdateVersion" -ne "$newUpdateVersion" ] || [ "$sgSid" -eq "0" ] ; then
	echo "svn已经更新，各部件重启.."
	echo "kill jetty 8800"
	jps -m|grep 8800|awk '{print $1}'|xargs kill -9
	echo "kill memcache.."
	str1=$(ps aux|grep "memcached"|grep 11311|awk '{print $2}')
	echo "The old memcach pid is"$str1

#kill memcached
	echo "killing memcache "
        ps aux|grep memcached|grep 11311|awk '{print $2}'|xargs kill -9

        echo  "start memcache"
#start memcached

        memcached -d -p 11311
        str2=$(ps aux|grep "memcached"|grep "11311"|awk '{print $2}')
        while [ "$str2" == "" ]
        do
                memcached -d -p 11311
                str2=$(ps aux|grep "memcached"|grep "11311"|awk '{print $2}')
        done
        echo "the new memcache pid is "$str2

        if [ "$str2" != "$str1" ] ; then
                echo "restart memcache 11311 successfully!"
        else
                echo "restart memcache 11311 faile!"
        fi
#设置grid里面的memcached
	sed -i /production.memcached.servers/s/11211/11311/ /home/lhy/project/guangxi_sg/guangxiTrunk/src/config/grid.properties
#切换jdbc数据库
	sed -i /production.url/s/192.168.10.146/192.168.100.52/ /home/lhy/project/guangxi_sg/guangxiTrunk/src/config/jdbc.properties
	#build 
	ant -f  production.build.xml compile build

	echo "start jetty"
	#start jetty
	ant -buildfile production.build.xml startJetty -Dport=8800 -Dlistenerport=8801 -Dpath=/ -Drootdir=./webroot
        sleep 240
	echo "jetty启动时间：™ `date`"
else
	echo -e "\033[46;30m svn未更新 \033[0m"
fi

